// This Adobe Illustrator script batches the Braille labeling process
// Requirements:
// 1. Image is "name.Resized.bmp"
// 2. The corresponding location file is "name.SelMBs.xml"
// 3. The corresponding label file is "name.Text.txt"
// 4. Files 1, 2, and 3 are in the same folder
// Note: Doesn't work recursively

// select folder with source files
var workFolder = Folder.selectDialog("Select work folder","~");

// select folder to save to
var targetFolder = Folder.selectDialog("Select target folder", "~");

// find images
if(workFolder != null)
{
	var bmpList = new Array();
	var bmpCount = 0;
	var fileList = workFolder.getFiles();
	for( i = 0; i <fileList.length; i++)
	{
		if(fileList[i] instanceof File)
		{
			filename = fileList[i].fsName;
			extension = getSubString(filename,null,".ai");
			if(extension != null)
			{
				bmpList.push(fileList[i]);
				bmpCount++;
			}
		}
	}
}

// label each image
for (i = 0; i < bmpCount; i++)
{
	docRef = open(bmpList[i]);
	var saveOpts = new IllustratorSaveOptions();
	saveOpts.embedLinkedFiles = true;
	
	// save current image
	theName = bmpList[i].name
	toSave = new File(targetFolder.fsName+"\\"+theName);
	docRef.saveAs(toSave,saveOpts);
	docRef.close();

}


// get the string between startTag and endTag
function getSubString (data, startTag, endTag)
{
   if( startTag != null) {
	start = data.indexOf(startTag);
	if(start == -1) return null;
	startLen = start+startTag.length;
   } else {
   	startLen = 0;	
   }
   
   if(endTag != null) {
   	end  = data.indexOf(endTag);
   	if(end == -1) return null;
   } else {
   	end = data.length;
   }
   return data.substring(startLen , end);
}